package uk.co.q3c.deplan.server.domain.util;

import java.util.Calendar;
import java.util.Date;

import uk.co.q3c.deplan.client.util.Q3DateUtil;
import uk.co.q3c.deplan.server.util.Q3Calendar;

public class Q3CalendarUtil extends Q3DateUtil {

	/**
	 * returns a GregorianCalendar set to just the date component of <code>date</code>, and its time component set to 0. If
	 * <code>date</code> is null, returns null
	 * 
	 * @param date
	 * @return
	 */
	public static Q3Calendar dateOnly_calendar(Date date) {
		if (date == null) {
			return null;
		}
		Q3Calendar cal = new Q3Calendar();
		cal.setTime(date);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		return cal;
	}

	public static Date dateOnly_date(Date date) {
		return dateOnly_calendar(date).getTime();
	}

	/**
	 * Returns a new calendar instance set to <code>date</code>, adjusted to the beginning of the day
	 * 
	 * @param date
	 * @return
	 */
	public static Q3Calendar startOfDay_calendar(Date date) {
		if (date == null) {
			return null;
		}
		Q3Calendar cal = new Q3Calendar();
		cal.setTime(date);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.HOUR_OF_DAY, 9);
		return cal;
	}

	/**
	 * @param cal
	 * @return
	 */
	public static String dateToKey(Calendar cal) {
		return dateToKey(cal.getTime());

	}

}
